import fs from 'node:fs'

const rootDirectory = './'
const readmePathInRoot = `./README.md`

await fs.promises.rm(readmePathInRoot)

const files = await fs.promises.readdir(rootDirectory)
for (const file of files) {
  const subDirectory = `${rootDirectory}${file}`
  const readmePathInSub = `${subDirectory}/README.md`
  const subDirectoryStat = await fs.promises.stat(subDirectory)
  if (subDirectoryStat.isDirectory() === false) {
    continue
  }
  const readmePathStat = await fs.promises.stat(readmePathInSub)
  if (readmePathStat.isFile() === false) {
    console.warn(`${file} does not have a README.md`)
    continue
  }
  const line = `1. [${file}](${file}/README.md)\n`
  await fs.promises.appendFile(readmePathInRoot, line)
}
